package com.clx.chat.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import com.clx.chat.config.RabbitMQConfig;
import com.clx.chat.handler.NettyServerHandler;
import com.clx.chat.mapper.ContactMapper;
import com.clx.chat.mapper.MessageMapper;
import com.clx.common.domain.entity.Message;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;


/**
 * 消息接收者
 */
@Service
@Slf4j
public class RabbitmqReceiver {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private ContactMapper contactMapper;

    /**
     * 监听用户A给离线的用户B发送的消息
     * @param
     */
    @RabbitListener(queues = {RabbitMQConfig.QUEUE_NAME},ackMode = "MANUAL")//ackMode手动确认消息被消费
    public void listenMessage(String message, com.rabbitmq.client.Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) {;
        Message javaObject = JSON.toJavaObject(message, Message.class);
        Integer receiverId = javaObject.getReceiverId();
        String content = javaObject.getContent();
        try {
            if (!NettyServerHandler.getOnlineStatus(receiverId)){
                //没上线
//                System.out.println("用户 {"+receiverId+"} 离线,  消息暂存: " + content);
                //拒绝消息消费
                channel.basicNack(tag,false,true);
            }else {
                //上线了
                Channel channel0 = NettyServerHandler.getChanelByUserId(receiverId);
                channel0.writeAndFlush(new TextWebSocketFrame(message));
                //手动确认消息
                channel.basicAck(tag,false);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    /**
     * 监听添加联系人成功的消息
     */
    @RabbitListener(queues = {"contact-que"},ackMode = "MANUAL")
    public void listenContact(Integer userId, com.rabbitmq.client.Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) {
        try {
            if (NettyServerHandler.getOnlineStatus(userId)){
                //如果在线
                Channel chanel = NettyServerHandler.getChanelByUserId(userId);
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("type","contact-add-finish");
                String s = JSON.toJSONString(jsonObject);
                chanel.writeAndFlush(new TextWebSocketFrame(s));
                //手动确认消息
                channel.basicAck(tag,false);
            }else {
                //不在线
                //拒绝消息消费
                channel.basicNack(tag,false,true);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }





}